<?php

namespace app\store\service\game;

use app\common\model\game\Log;
use app\common\service\BaseService;
use app\store\model\game\Game as GameModel;
use app\common\model\game\Log as GameLogModel;

class Game extends BaseService
{
    /**
     * 游戏列表
     */
    public function getList($params):array
    {
        $condition = [];
        if (!empty($params['status'])) {
            if($params['status'] != 'all'){
                $condition[] = ['status', '=', $params['status']];
            }
        }
        if (!empty($params['name'])) {
            $condition[] = ['name', 'like', "%{$params['name']}%"];
        }
        $condition[] = ['is_del','=',0];
        return (new GameModel())
            ->with(['room','category'])
            ->where($condition)
            ->order(['sort' => 'DESC','created_at' => 'DESC'])
            ->paginate($params['limit'] ?? 15)
            ->toArray();
    }


    /**
     * 保存游戏
     */
    public function save($params): bool
    {
        if(empty($params['id'])){
            $game = (new GameModel())::create([
                'room_id' => $params['room_id'],
                'cover_url' => $params['cover_url'],
                'desc' => $params['desc'],
                'category_id' => $params['category_id'],
                'sort' => $params['sort'] ?? 100,
                'remark' => $params['remark'] ?? ''
            ]);
            if(!$game->id){
                throwError('insert game error');
            }
        }else{
            $condition = [
                ['id','=',$params['id']],
                ['is_del','=',0]
            ];
            $game = (new GameModel())->where($condition)->find();
            if(!$game){
                throwError('游戏不存在');
            }
            $game->room_id = $params['room_id'];
            $game->cover_url = $params['cover_url'];
            $game->desc = $params['desc'];
            $game->category_id = $params['category_id'] ?? 0;
            $game->sort = $params['sort'] ?? 100;
            $game->remark = $params['remark'] ?? '';
            $game->save();
        }

        return true;
    }



    /** 游戏详情
     * @param $id
     * @return array
     */
    public function detail($id):array
    {
        $condition = [
            ['id','=',$id],
            ['is_del','=',0]
        ];
        $game =(new GameModel())->with(['room'])->where($condition)->find();
        if(!$game){
            throwError('游戏不存在');
        }
        $game->table_id = json_decode($game['table_id'],true);
        return $game->toArray();
    }



    /**删除游戏
     * @param $id
     * @return bool
     */
    public function delete($id):bool
    {
        $condition = [
            ['id','=',$id],
            ['is_del','=',0]
        ];
        $game =(new GameModel())->with(['room'])->where($condition)->find();
        if(!$game){
            throwError('游戏不存在');
        }
        $game->is_del = 1;
        $game->save();
        return true;
    }

    /**游戏日志
     * @param $params
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getLog($params):array
    {
        return (new GameLogModel())
            ->with(['room'])
            ->where([])
            ->paginate($params['limit'] ?? 15)
            ->toArray();
    }


}